'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async collcetioncreate() {
    const { ctx } = this;
    const { uid, releaseid, tutorialid, fieldid, appointmentid } = ctx.request.body;
    if (!uid) {
      return ctx.body = { success: false, msg: '请传入uid' };
    }
    if (!(releaseid || tutorialid || fieldid || appointmentid)) {
      return ctx.body = { success: false, msg: '请传入收藏的作品id' };
    }
    const create = {};
    create.uid = uid;
    if (releaseid) {
      create.releaseid = releaseid;
    }
    if (tutorialid) {
      create.tutorialid = tutorialid;
    }
    if (fieldid) {
      create.fieldid = fieldid;
    }
    if (appointmentid) {
      create.appointmentid = appointmentid;
    }
    try {
      await ctx.model.Collection.create(
        create
      );
      ctx.body = { success: true, msg: '收藏成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '收藏失败' };
    }
  }

  async collcetiondelete() {
    const { ctx } = this;
    const { collectionid, uid, releaseid, tutorialid, fieldid, appointmentid } = ctx.request.body;
    const where = {};
    if (!(collectionid || uid)) {
      return ctx.body = { success: false, msg: '请传入必要查询字段' };
    }
    if (collectionid) {
      where.collectionid = collectionid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      await ctx.model.Collection.destroy({
        where,
      });
      ctx.body = { success: true, msg: '取消收藏' };
    } catch (e) {
      ctx.body = { success: false, msg: '取消收藏失败' };
    }
  }

  async collcetionfind() {
    const { ctx } = this;
    const { collectionid, uid, releaseid, tutorialid, fieldid, appointmentid, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    const where = {};
    if (collectionid) {
      where.collectionid = collectionid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      await ctx.model.Collection.hasOne(ctx.model.Release, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Collection.hasOne(ctx.model.Tutorial, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Collection.hasOne(ctx.model.Field, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Collection.hasOne(ctx.model.Appointment, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Release.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Release.hasMany(ctx.model.Picture, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Thumbs, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Comment, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Collection, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });


      await ctx.model.Tutorial.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Picture, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Thumbs, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Comment, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Collection, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });


      await ctx.model.Field.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Field.hasMany(ctx.model.Picture, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Thumbs, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Comment, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Collection, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });


      await ctx.model.Appointment.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Picture, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Thumbs, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Comment, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Collection, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      const res = await ctx.model.Collection.findAndCountAll({
        where,
        limit,
        offset,
        include: [{
          model: ctx.model.Release,
          include: [{
            model: ctx.model.User,
            attributes: {
              exclude: [ 'pas' ],
            },
          }, {
            model: ctx.model.Picture,
          }, {
            model: ctx.model.Thumbs,
          }, {
            model: ctx.model.Comment,
          }, {
            model: ctx.model.Collection,
          }],
        }, {
          model: ctx.model.Tutorial,
          include: [{
            model: ctx.model.User,
            attributes: {
              exclude: [ 'pas' ],
            },
          }, {
            model: ctx.model.Picture,
          }, {
            model: ctx.model.Thumbs,
          }, {
            model: ctx.model.Comment,
          }, {
            model: ctx.model.Collection,
          }],
        }, {
          model: ctx.model.Field,
          include: [{
            model: ctx.model.User,
            attributes: {
              exclude: [ 'pas' ],
            },
          }, {
            model: ctx.model.Picture,
          }, {
            model: ctx.model.Thumbs,
          }, {
            model: ctx.model.Comment,
          }, {
            model: ctx.model.Collection,
          }],
        }, {
          model: ctx.model.Appointment,
          include: [{
            model: ctx.model.User,
            attributes: {
              exclude: [ 'pas' ],
            },
          }, {
            model: ctx.model.Picture,
          }, {
            model: ctx.model.Thumbs,
          }, {
            model: ctx.model.Comment,
          }, {
            model: ctx.model.Collection,
          }],
        }],
        order: [[ 'collectionid', 'DESC' ]],
      });
      const length = await ctx.model.Collection.findAll({
        where,
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }
}

module.exports = HomeController;
